package cn.wenhe9.gulimall.oss.controller;

import cn.wenhe9.common.utils.ResultResponse;
import cn.wenhe9.gulimall.oss.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author DuJinliang
 * 2022/4/13
 */
@Api(tags = "文件上传管理接口")
@RestController
@RequestMapping("/api/oss/file")
public class FileApiController {

    @Resource
    private FileService fileService;

    /**
     * 获取服务端上传签名
     * @return ResultResponse
     */
    @ApiOperation("获取服务端上传签名")
    @GetMapping("/policy")
    public ResultResponse getPolicy(){
        String uploadToken = fileService.getUploadToken();
        return ResultResponse.ok(uploadToken);
    }

    @PostMapping("/upload")
    public ResultResponse upload(MultipartFile file) throws Exception {
        String url = fileService.uploadFile(file);
        return ResultResponse.ok(url);
    }
}
